package com.ktjy.web.dao.impl;


import com.ktjy.web.dao.BaseDao;
import com.ktjy.web.dao.BrandDao;
import com.ktjy.web.dao.UserDao;
import com.ktjy.web.entity.BrandItem;
import com.ktjy.web.entity.Page;
import com.ktjy.web.entity.User;
import com.ktjy.web.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BrandDaoImpl extends BaseDao implements BrandDao {

    @Override
    public List<BrandItem> selectAll() {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        List<BrandItem> list=null;
        try{
            list=sqlSession.getMapper(BrandDao.class).selectAll();

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return list;
    }

    @Override
    public BrandItem selectById(int id) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        BrandItem brandItem=null;
        try{
            brandItem=sqlSession.getMapper(BrandDao.class).selectById(id);

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return brandItem;
    }

    @Override
    public int insert(BrandItem brandItem) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        int count=0;
        try{
            count=sqlSession.getMapper(BrandDao.class).insert(brandItem);

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }

    @Override
    public int update(BrandItem brandItem) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        int count=0;
        try{
            count=sqlSession.getMapper(BrandDao.class).update(brandItem);

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }

    @Override
    public int deleteById(int id) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        int count=0;
        try{
            count=sqlSession.getMapper(BrandDao.class).deleteById(id);

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }

    @Override
    public List<BrandItem> page(String name, int pageNo, int pageSize) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        List<BrandItem> list=null;
        try{
            list=sqlSession.getMapper(BrandDao.class).page(name,pageNo,pageSize);

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return list;
    }

    @Override
    public int count() {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        int count=0;
        try{
            count=sqlSession.getMapper(BrandDao.class).count();

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }
}
